(function() {
    appCommon.controller('loginCtr', loginController);
    appCommon.controller('registerCtr', registerController);
    appCommon.controller('loginDefaultCtr', loginDefaultController);   

    //登录
    function loginController($scope, $rootScope, $http, $httpParamSerializer) { 
        $scope.save = function() {
            if($scope.userName) {
                $http({
                    method: 'POST',
                    url: '/index.php?m=home&a=login&c=login',
                    data: $httpParamSerializer({
                        'userName': $scope.userName,
                        'password': $scope.password
                    }),
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    var data = response.data;
                    if(data.status === 1) {        
                        window.location.href="/"
                    }else if(data.status === 0) {
                        if(data.type === "userNameError") {
                            $scope.userNameError = "show";
                            $scope.passwordError = "";
                            $scope.userNameMessage = data.message;
                        }else if(data.type === "passwordError") {
                            $scope.userNameError = "";
                            $scope.passwordError = "show";
                            $scope.passwordMessage = data.message;
                        }
                    }
                }, function errorCallback(response) {
                    alert(response);
               });
            }
        }
    };

    //注册
    function registerController($scope, $http, $httpParamSerializer, ngDialog) {
        $scope.save = function() {
            if($scope.userName) {
                $http({
                    method: 'POST',
                    url: '/index.php?m=home&a=register&c=login',
                    data: $httpParamSerializer({
                        'userName': $scope.userName,
                        'password': $scope.password,
                        'email': $scope.email
                    }),
                    headers:{'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    $scope.emailError = $scope.userNameError =  $scope.passwordError = false;
                    var data = response.data;
                    if(data.status === 1) {
                        $scope.saveMassage = data["message"];

                        var dialog = ngDialog.open({      
                            plain: true,                    
                            template: '<div class="save-massage">'+$scope.saveMassage+'</div>', 
                            controller: 'registerCtr',
                            overlay: false
                        });
                        //2秒后关闭
                        setTimeout(function(){
                            dialog.close();
                            window.location.href="/";
                        },  2000);

                    }else if(data.status === 0){
                        $scope.emailError = $scope.userNameError =  $scope.passwordError = false;
                        if(data.type === "userNameError") {
                            $scope.userNameError = true;
                            $scope.userNameMessage = data.message;
                        }else if(data.type === "passwordError") {
                            $scope.userNameError = "";
                            $scope.passwordError = true;
                            $scope.passwordMessage = data.message;                            
                        }else if(data.type === "emailError") {
                            $scope.emailError = "";
                            $scope.emailError = true;
                            $scope.emailMessage = data.message;  
                        }
                    }
                }, function errorCallback(response) {
                    console.log(response)
                });
            }
        }

        $scope.showTip = function() {
            var dialog = ngDialog.open({      
                template: 'tip.html', 
                overlay: false,
                width: 500,
                height: 500
            });
        }
    };

    //判断是否  已登录直接跳转到首页
    function loginDefaultController($http, $scope, ngDialog) {  
        ngDialog.close();
        $http({
            method: 'POST',
            url: '/index.php?m=home&a=CheckLogin&c=login',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            var data = response.data;
            if(data) {        
                window.location.href="/"
            }
        }) 
       
    }
})();
